#define _CRT_SECURE_NO_WARNINGS 1


#include"stack.h"



void StackInit(struct Stack* ps)
{
	assert(ps);
	ps->arr = (StackData*)malloc(sizeof(StackData) * 4);
	ps->top = 0;
	ps->capacity = 4;
	if (ps->arr == NULL)
	{
		perror(ps->arr);
		exit(-1);
	}
}

void StackDestory(struct Stack* ps)
{
	free(ps->arr);
	ps->arr = NULL;
	ps->top = 0;
	ps->capacity = 0;
}



void StackPush(struct Stack* ps, StackData x)
{
	assert(ps);


	

	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->capacity = newcapacity;
		StackData* a = (StackData*)realloc(ps->arr, sizeof(StackData) * ps->capacity);
		ps->arr = a;
	}

	ps->arr[ps->top] = x;
	ps->top++;



}




void StackPop(struct Stack* ps)
{
	assert(ps->top > 0);
	ps->top--;
}


void StackPrintf(struct Stack* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->top; i++)
	{
		printf("%d ", ps->arr[i]);
	}
}


bool StackEmpty(struct Stack* ps)
{
	assert(ps);

	/*if (ps->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}*/

	return ps->top == 0;
}



StackData StackTop(struct Stack* ps)
{
	assert(ps);

	assert(!StackEmpty(ps));
	

	return ps->arr[ps->top - 1];
}


int StackSize(struct Stack* ps)
{
	assert(ps);

	return ps->top;
}
